package com.dj.mingkong.common.util;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.NoSuchAlgorithmException;

public final class AESUtils {
    private static final Logger log = LoggerFactory.getLogger(AESUtils.class);

    public AESUtils() {
    }

    public static String encrypt(String content, String key) {
        String encryptStr = null;

        try {
            Cipher aesEcb = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
            aesEcb.init(1, secretKeySpec);
            byte[] result = aesEcb.doFinal(content.getBytes());
            encryptStr = Base64.encodeBase64URLSafeString(result);
        } catch (Exception var6) {
            log.error(var6.getMessage(), var6);
        }

        return encryptStr;
    }

    public static String decrypt(String content, String key) {
        String decryptStr = null;

        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
            cipher.init(2, secretKeySpec);
            byte[] result = Base64.decodeBase64(content);
            decryptStr = new String(cipher.doFinal(result));
        } catch (Exception var6) {
            log.error(var6.getMessage(), var6);
        }

        return decryptStr;
    }

    public static String cbcEncrypt(String source, String secretKey, String secondKey, String outType) throws Exception {
        if (checkSecretKey(secretKey)) {
            return null;
        } else {
            byte[] raw = secretKey.getBytes(StandardCharsets.UTF_8);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] bytes = secondKey.getBytes();
            IvParameterSpec iv = new IvParameterSpec(bytes);
            cipher.init(1, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(source.getBytes());
            return "base64".equalsIgnoreCase(outType) ? Base64.encodeBase64String(encrypted) : HexUtils.byteToHex(encrypted);
        }
    }

    public static String cbcDecrypt(String source, String secretKey, String slatKey, String outType) throws Exception {
        if (checkSecretKey(secretKey)) {
            return null;
        } else {
            byte[] raw = secretKey.getBytes(StandardCharsets.UTF_8);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] bytes = slatKey.getBytes();
            IvParameterSpec iv = new IvParameterSpec(bytes);
            cipher.init(2, skeySpec, iv);
            byte[] encrypted1 = "base64".equalsIgnoreCase(outType) ? Base64.decodeBase64(source) : HexUtils.hexToBytes(source);

            try {
                byte[] original = cipher.doFinal(encrypted1);
                return new String(original);
            } catch (Exception var11) {
                log.error("AES decode fail", var11);
                return null;
            }
        }
    }

    private static boolean checkSecretKey(String secretKey) {
        if (secretKey == null) {
            log.info("Key为空null");
            return true;
        } else if (secretKey.length() < 16) {
            log.info("Key长度不是16位");
            return true;
        } else {
            return false;
        }
    }

    public static String generateKey() {
        String genKey = null;

        try {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            keygen.init(128);
            Key key = keygen.generateKey();
            byte[] keyBytes = key.getEncoded();
            genKey = Base64.encodeBase64String(keyBytes);
        } catch (NoSuchAlgorithmException var4) {
            log.error(var4.getMessage(), var4);
        }

        return genKey;
    }
}
